package msc.ai.mas.creditcard;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PaymentGatewayGui extends JFrame
{
	private PaymentGatewayAgent myPGAgent;
	
	private JTextField txtCCNummber,txtCVVNummber,txtCCType;
	private JTextField txtUserName,txtAddLine1,txtAddLine2,txtAddCity,txtAddState,txtAddZipCode,txtAddCountry;
	private JTextField txtCCLimit,txtCurrentBalance,txtExpireYear,txtExpireMonth,txtCCStatus;
	
	
	public PaymentGatewayGui(PaymentGatewayAgent pgAgent)
	{
		super(pgAgent.getLocalName());
		this.myPGAgent = pgAgent;
		
		JPanel p = new JPanel();		
		p.setLayout(new GridLayout(9 , 3));
		
		p.add(new JLabel("Acc Holder Name:"));
		txtUserName = new JTextField(15);
		p.add(txtUserName);
		p.add(new JLabel(""));
		
		p.add(new JLabel("Address Line 1 & 2 :"));
		txtAddLine1 = new JTextField(15);
		p.add(txtAddLine1);
		txtAddLine2 = new JTextField(15);
		p.add(txtAddLine2);
		
		p.add(new JLabel("City and Zip Code :"));
		txtAddCity = new JTextField(15);
		p.add(txtAddCity);
		txtAddZipCode = new JTextField(15);
		p.add(txtAddZipCode);
		
		p.add(new JLabel("State and Country:"));
		txtAddState = new JTextField(15);
		p.add(txtAddState);
		txtAddCountry = new JTextField(15);
		p.add(txtAddCountry);
		
		p.add(new JLabel("Credit Card Number and CVV :"));
		txtCCNummber = new JTextField(15);
		p.add(txtCCNummber);		
		txtCVVNummber = new JTextField(15);
		p.add(txtCVVNummber);
		
		p.add(new JLabel("Credit Card Type && Status:"));
		txtCCType = new JTextField(15);		
		p.add(txtCCType);
		txtCCStatus = new JTextField(15);		
		p.add(txtCCStatus);
		
		p.add(new JLabel("Expire Year and Expire Month:"));
		txtExpireYear = new JTextField(15);		
		p.add(txtExpireYear);
		txtExpireMonth = new JTextField(15);		
		p.add(txtExpireMonth);
		
		p.add(new JLabel("Credit Limit and Current Balance:"));
		txtCCLimit = new JTextField(15);		
		p.add(txtCCLimit);
		txtCurrentBalance= new JTextField(15);		
		p.add(txtCurrentBalance);
		
		p.add(new JLabel(""));
		p.add(new JLabel(""));
		p.add(new JLabel(""));
		
		getContentPane().add(p, BorderLayout.CENTER);
		
		
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(1 , 2));
		
		JButton btnPrintList = new JButton("Print CC List");
		btnPrintList .addActionListener( new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				try 
				{
					System.out.println("Credit Card List of Gateway : " + myPGAgent.getLocalName() );
					
					HashMap<String, CreditCard> ccList = myPGAgent.getCreditCardList();
					int i = 1;
					
					Set<String> keySet = ccList.keySet();
					
					for (String key : keySet)
					{
						CreditCard cc  = ccList.get(key);
						System.out.println(i + ":" + cc.getCreditCardNo() + "- status : " + cc.getCcStatus() + ", bal : " + cc.getCurrentBalance());
					}
										
				}
				catch (Exception e) 
				{
					JOptionPane.showMessageDialog(PaymentGatewayGui.this, "Error Printing. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		} );
		
		p2.add(btnPrintList);
		
		JButton btnAddItem = new JButton("Add Credit Card");
		btnAddItem.addActionListener( new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				try 
				{
					String ccNo = txtCCNummber.getText().trim();
					String ccCvv = txtCVVNummber.getText().trim();
					String ccType = txtCCType.getText().trim();					
					String ccUName = txtUserName.getText().trim();
					String ccAddL1 = txtAddLine1.getText().trim();
					String ccAddL2 = txtAddLine2.getText().trim();
					String ccAddCty = txtAddCity.getText().trim();
					String ccAddState = txtAddState.getText().trim();
					String ccAddZip = txtAddZipCode.getText().trim();
					String ccAddCtry = txtAddCountry.getText().trim();
					String ccLimitStr = txtCCLimit.getText().trim();
					String ccCurBalStr = txtCurrentBalance.getText().trim();
					String ccExpYrStr = txtExpireYear.getText().trim();
					String ccExpMthStr = txtExpireMonth.getText().trim();
					String ccStatusStr = txtCCStatus.getText().trim();
					
					CreditCard cc = new CreditCard(ccNo, Double.parseDouble(ccLimitStr), Double.parseDouble(ccCurBalStr), Integer.parseInt(ccStatusStr));
					cc.setAddLine1(ccAddL1);
					cc.setAddLine2(ccAddL2);
					cc.setAddZipCode(ccAddZip);
					cc.setAddCity(ccAddCty);
					cc.setAddState(ccAddState);
					cc.setAddCountry(ccAddCtry);
					
					cc.setCreditCardType(ccType);
					cc.setCreditCVVNo(ccCvv);
					
					cc.setExpireYear(Integer.parseInt(ccExpYrStr));
					cc.setExpireMonth(Integer.parseInt(ccExpMthStr));
					
					
					myPGAgent.addCreditCard(cc);
					
					txtCCNummber.setText("");
					txtCVVNummber.setText("");
					txtCCType.setText("");
					txtUserName.setText("");
					txtAddLine1.setText("");
					txtAddLine2.setText("");
					txtAddCity.setText("");
					txtAddState.setText("");
					txtAddZipCode.setText("");
					txtAddCountry.setText("");
					txtCCLimit.setText("");
					txtCurrentBalance.setText("");
					txtExpireYear.setText("");
					txtExpireMonth.setText("");
					txtCCStatus.setText("");
					
				}
				catch (Exception e) 
				{
					JOptionPane.showMessageDialog(PaymentGatewayGui.this, "Invalid values. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		} );
//		p = new JPanel();
		p2.add(btnAddItem);
		
		
		getContentPane().add(p2, BorderLayout.SOUTH);
		
		// Make the agent terminate when the user closes 
		// the GUI using the button on the upper right corner	
		addWindowListener(new	WindowAdapter() 
		{
			public void windowClosing(WindowEvent e) 
			{
				myPGAgent.doDelete();
			}
		} );
		
		setResizable(false);
	} 
	
	public void showGui() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		PaymentGatewayGui gui = new PaymentGatewayGui(new PaymentGatewayAgent());
		gui.showGui();
	}
}
